package cn.com.bdqn.controller;

import cn.com.bdqn.entity.CstCustomer;
import cn.com.bdqn.service.CstCustomerService;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cstCustomer")
@Api(tags = "客户信息管理接口")
public class CstCustomerController {

    @Autowired
    private CstCustomerService cstCustomerService;

    @Operation(summary = "客户信息高级查询")
    @PostMapping("/queryByLike")
    public ReturnData queryByLike(CstCustomer customer, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "15") int pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<CstCustomer> cstCustomers = cstCustomerService.queryByLike(customer);
            PageInfo pageInfo = new PageInfo(cstCustomers,3);
            return ReturnData.ok().put("cstCustomers",cstCustomers).put("pageInfo",pageInfo);
        }catch (Exception e){
            return ReturnData.currError("客户信息高级查询异常");
        }
    }

    @Operation(summary = "新增客户信息")
    @PostMapping("/addCstCustomer")
    public ReturnData addCstCustomer(CstCustomer customer){
        int result = cstCustomerService.addCstCustomer(customer);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("新增客户信息异常");
        }
    }

    @Operation(summary = "根据客户编号查询客户信息")
    @PostMapping("/queryById")
    public ReturnData queryByID(String custNo){
        try {
            CstCustomer cstCustomer = cstCustomerService.queryByCustNo(custNo);
            return ReturnData.ok().put("cstCustomer",cstCustomer);
        }catch (Exception e){
            return ReturnData.currError("单个客户信息查询异常");
        }
    }

    @Operation(summary = "生成客户编号")
    @PostMapping("/createNewCustNo")
    public ReturnData createNewCustNo() {
        //格式说明 KH231111xxx KH+当前年月日+编号（具体长度看需求）
        String front="KH";//前缀
        //当前时间编码
        Date date = new Date();
        String bidDate = new SimpleDateFormat("yyMMdd").format(date);

        CstCustomer cstCustomer = cstCustomerService.queryByDate(bidDate);
        if (cstCustomer != null){// 在数据表中查到了，说明现在这个订单不是今天的第一单
            String bid = cstCustomer.getCustNo(); //取出ID，也就是业务号
            bid = bid.substring(8,11); // 取出后三位数，也就是自动生成的三位数 001
            int num = Integer.parseInt(bid);
            num ++;  // 加1
            if(num<10){
                String bidNum = String.format("%03d", num);//%03d 只是三位，不足补0
                String code = front+bidDate+bidNum;
                return ReturnData.ok().put("code",code);
            }
            else if(num<100){
                String bidNum = String.format("%03d", num);//num<100,说明是两位数，前面要补一个0
                String code = front+bidDate+bidNum;
                return ReturnData.ok().put("code",code);
            }
            else {
                String bidNum = String.valueOf(num);
                String code =front+bidDate+bidNum;
                return ReturnData.ok().put("code",code);
            }
        }else {
            int number = 1;
            String bidNum = "00" + number;
            String code = front+bidDate+bidNum;
            return ReturnData.ok().put("code",code);
        }
    }

    @Operation(summary = "修改客户信息")
    @PostMapping("/updateCstCustomer")
    public ReturnData updateCstCustomer(CstCustomer customer){
        int result = cstCustomerService.updateCstCustomer(customer);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("修改客户信息异常");
        }
    }

}
